import fs from "node:fs";
import path from "node:path";
import revisionHash from "rev-hash";

let result = revisionHash(fs.readFileSync("./scripts/lottery.js"));
//=> 'bb9d8fe615'
console.log("lottery.js==>", result);
//获取当前目录父目录
let parentPath = path.resolve(".");
console.log("parentPath==>", parentPath);
const ignoreDir = [".git", "node_modules"];
function isIgnoreDir(dir) {
  //dir 文件名
  let fileName = path.basename(dir);
  console.log("dir==>", fileName);
  return ignoreDir.includes(fileName);
}
function getAllFiles(file) {
  if (fs.statSync(file).isDirectory() && !isIgnoreDir(file)) {
    //遍历当前目录下所有文件
    fs.readdirSync(file).forEach((f) => {
      getAllFiles(path.join(file, f));
    });
  } else {
    if (path.extname(file) === ".js" || path.extname(file) === ".css") {
      let result = revisionHash(fs.readFileSync(file));
      console.log(file + "==>", result);
    }
  }
}
getAllFiles(parentPath);
